package sctu.edu.java.year2016.team05.wangliwei.day20160530.chapter11_3;

/**
 *
 创建Computer类 该类中有一个计算两个数的最大公约数的方法，如果向该方法传递负整数，该方法就会抛出自定义异常。
 */
import java.util.Scanner;
public class TryTest {
    public static void main(String[] args) {
        Computer computer=new Computer();
        try{
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入第一个数的值");
            int a = scanner.nextInt();
            System.out.println("请输入第二个数的值");
            int b = scanner.nextInt();
            computer.count(a,b);
        }catch (MyException ex){
            System.out.println("错误描述"+ex.toString());


        }
    }

}

class MyException extends Exception{

    String message;
    public MyException(int a,int b) {
        message="您输入了负整数";  }
    public String toString(){
        return message;  }
}
class Computer {
    public void count(int a, int b) throws MyException {
        if (a == 1 || b == 1) System.out.println("最大公约数为1");

        if (a < 0 || b < 0) {
            throw new MyException(a, b);
        } else {
            int x = a;
            for (int i = x; i > 1; i--) {

                int c;
                if (a % i == 0 && b % i == 0) {
                    c = i;
                    System.out.println("a和b的最大公约数是" + c);

                }

            }

        }
    }
}